{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 注意！请根据提示操作！\n",
    "\n",
    "如果当页面加载完成，你还是看到了这条提示\n",
    "\n",
    "那么你的notebook并未被信任，导致代码无法正常运行\n",
    "\n",
    "点击右上角的`不可信`，将本notebook列入可信列表，即可正常运行。\n",
    "\n",
    "![可信按钮](./mgr/trust.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 【项目】小学算术题训练助手\n",
    "\n",
    "本章代码会带领你制作完成一个算术题助手。\n",
    "\n",
    "首先思考小学生算术题需要怎么样的难度，确定其制作范围：\n",
    "\n",
    "1. 四则运算加减乘除\n",
    "2. 加减总和不大于100，不能有负数\n",
    "3. 乘法限于个位数\n",
    "4. 除法限于个位数，且能整除\n",
    "\n",
    "我们可以先做一个最简单的加法题："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = 1 + 2\n",
    "print('1 + 2 = ?')\n",
    "i = input()\n",
    "if i == str(result):\n",
    "    print('回答正确！')\n",
    "else:\n",
    "    print('回答错误！')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用random模块可以获取随机数字，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "a = random.randint(0, 100)  # 获取0到100的随机整数\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "结合起来，可以作出随机的加法题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "a = random.randint(0, 100)\n",
    "b = random.randint(0, 100)\n",
    "result = a + b\n",
    "print(f'{a} + {b} = ?')\n",
    "i = input()\n",
    "if i == str(result):\n",
    "    print('回答正确！')\n",
    "else:\n",
    "    print('回答错误！')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但是加法的和不能超过100，所以我们需要稍微处理下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "a = random.randint(0, 100)\n",
    "b = random.randint(0, 100 - a)\n",
    "result = a + b\n",
    "print(f'{a} + {b} = ?')\n",
    "i = input()\n",
    "if i == str(result):\n",
    "    print('回答正确！')\n",
    "else:\n",
    "    print('回答错误！')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接下去要制作加减乘除的随机选取功能，可以用如下方式进行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "symbol = random.randint(0, 3)  # 0到3，一共4个数字，分别代表加减乘除\n",
    "if symbol == 0:  # 加\n",
    "    a = random.randint(0, 100)\n",
    "    b = random.randint(0, 100 - a)\n",
    "    result = a + b\n",
    "    quiz = f'{a} + {b} = ?'\n",
    "elif symbol == 1:  # 减\n",
    "    a = random.randint(0, 100)\n",
    "    b = random.randint(0, 100)\n",
    "    a, b = max(a, b), min(a, b)  # 减法不能出现负数，所以需要把大的数字放到a的地方，小的数字放到b的地方\n",
    "    result = a - b\n",
    "    quiz = f'{a} - {b} = ?'\n",
    "elif symbol == 2:  # 乘\n",
    "    a = random.randint(0, 10)\n",
    "    b = random.randint(0, 10)\n",
    "    result = a * b\n",
    "    quiz = f'{a} x {b} = ?'  # 用x代替*号显示出来，方便用户理解\n",
    "else:\n",
    "    result = random.randint(0, 10)\n",
    "    b = random.randint(0, 10)\n",
    "    a = result * b  # 因为要取整，反向用结果去倒求题目比较好。\n",
    "    quiz = f'{a} / {b} = ?'\n",
    "print(quiz)\n",
    "i = input()\n",
    "if i == str(result):\n",
    "    print('回答正确！')\n",
    "else:\n",
    "    print('回答错误！')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最后再加上一个循环，可以反复练习："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "for _ in range(5):  # 连续出5道题目，下划线变量一般意味着这个变量接下去不用。\n",
    "    symbol = random.randint(0, 3)  # 0到3，一共4个数字，分别代表加减乘除\n",
    "    if symbol == 0:  # 加\n",
    "        a = random.randint(0, 100)\n",
    "        b = random.randint(0, 100 - a)\n",
    "        result = a + b\n",
    "        quiz = f'{a} + {b} = ?'\n",
    "    elif symbol == 1:  # 减\n",
    "        a = random.randint(0, 100)\n",
    "        b = random.randint(0, 100)\n",
    "        a, b = max(a, b), min(a, b)  # 减法不能出现负数，所以需要把大的数字放到a的地方，小的数字放到b的地方\n",
    "        result = a - b\n",
    "        quiz = f'{a} - {b} = ?'\n",
    "    elif symbol == 2:  # 乘\n",
    "        a = random.randint(0, 10)\n",
    "        b = random.randint(0, 10)\n",
    "        result = a * b\n",
    "        quiz = f'{a} x {b} = ?'  # 用x代替*号显示出来，方便用户理解\n",
    "    else:\n",
    "        result = random.randint(0, 10)\n",
    "        b = random.randint(0, 10)\n",
    "        a = result * b  # 因为要取整，反向用结果去倒求题目比较好。\n",
    "        quiz = f'{a} / {b} = ?'\n",
    "    print(quiz)\n",
    "    i = input()\n",
    "    if i == str(result):\n",
    "        print('回答正确！')\n",
    "    else:\n",
    "        print('回答错误！')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 思考\n",
    "\n",
    "请你思考下，能否再加上评分功能？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<hr>\n",
       "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/mdui/0.4.3/css/mdui.min.css\">\n",
       "<script src=\"./mgr/submit.js\"></script>\n",
       "<script src=\"./mgr/hide.js\"></script>\n",
       "<div class=\"mdui-container-fluid\">\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-500\" onclick=\"next_page(-1)\">上一章</button>\n",
       "    </div>\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-700\" onclick=\"window.open('/tree')\">目录</button>\n",
       "    </div>\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-900\" onclick=\"next_page(1)\">下一章</button>\n",
       "    </div>\n",
       "</div>\n",
       "<div id=\"html_output\">\n",
       "</div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<hr>\n",
    "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/mdui/0.4.3/css/mdui.min.css\">\n",
    "<script src=\"./mgr/submit.js\"></script>\n",
    "<script src=\"./mgr/hide.js\"></script>\n",
    "<div class=\"mdui-container-fluid\">\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-500\" onclick=\"next_page(-1)\">上一章</button>\n",
    "    </div>\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-700\" onclick=\"window.open('/tree')\">目录</button>\n",
    "    </div>\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-900\" onclick=\"next_page(1)\">下一章</button>\n",
    "    </div>\n",
    "</div>\n",
    "<div id=\"html_output\">\n",
    "</div>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
